package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 李旭
 * @date 2021/10/26 15:05
 * @Description:
 *
 *  基于插件的延迟消息的 Java配置类
 */
@Configuration
public class DelayMessageConfig {

    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    //交换机
    // direct:定向交换机 路由模型     fanout:扇出交换机 发布、订阅模型  topic:主题交换机 主题模型

    @Bean
    public Exchange delayExchange(){
        //x-delayed-type : CustomExchange自定义交换机的类型        交换机类型
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        //x-delayed-message : 自定义交换机中延迟消息的类型          交换机中的消息类型
        return new CustomExchange(exchange_delay,"x-delayed-message",
                true,false,arguments);
    }
    //普通队列
    @Bean
    public Queue delayQueue(){
        return QueueBuilder.durable(queue_delay_1).build();
    }
    @Bean
    public Binding bindingDelayQueueToDelayExchange(@Qualifier(value = "delayQueue") Queue delayQueue,
                                                    @Qualifier(value = "delayExchange") Exchange delayExchange){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(routing_delay).noargs();
    }

}
